有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

创建对象期间出现安卓 Java空指针异常

一切似乎都很好,但我无法在另一个类中创建新对象。我只是"java.lang.Null.Pointer.Exception" 实际上,我有一个F16类,用于创建F16飞机。现在,当我试图在MainActivity中创建一个新对象来显示飞机时,会显示这个错误

main活动类

public class MainActivity extends Activity {    
    LinearLayout Map=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Map=(LinearLayout)findViewById(R.id.Map);
        try{
        F16 myF16=new F16();        
        myF16.CreateAirCraft(myF16.CoordinateX,myF16.CoordinateY, this, Map);
        }
        catch(Exception ex){
        Log.d("Er", ex.toString());     
        }
    }

F16级

public class F16 extends AirCraft {
Context context;
private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
TextView PlaneName=null;// mentions to AirCraft name
ImageView PlaneImg=null;//mentions to AirCraft Image
public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
        Name="F-16";    
        PlaneBody=new LinearLayout(Ctx);
        PlaneName=new TextView(Ctx);
        PlaneImg=new ImageView(Ctx);
        PlaneName.setText(Name);
        PlaneImg.setImageResource(Image);
        PlaneBody.addView(PlaneName);
        PlaneBody.addView(PlaneImg);
        Map.addView(PlaneBody);
    }
}

飞机等级

public abstract class AirCraft {
   public String Name="";
   public byte CoordinateX=0;
   public byte CoordinateY=0;
   public byte Weight=0;
}

共 (3) 个答案

  1. # 1 楼答案

    在F16类中使用上下文作为参数生成构造函数 比如

    public F16(Context context)
    {
    Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
    }
    

    从主要活动传递上下文

    F16 myF16=new F16(this); 
    
  2. # 2 楼答案

    NullPointerException是由F16类的第3行引起的

    Context context;
    private int Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
    

    字段context被隐式初始化为null。然后Image被初始化并调用context.getResources(),但是由于context是空的,所以您得到了NPE

  3. # 3 楼答案

    试试下面的代码

    public class F16 extends AirCraft {
    
    Context context;
    private int Image;
    LinearLayout PlaneBody=null;//a container used to hold All information of Aircraft
    TextView PlaneName=null;// mentions to AirCraft name
    ImageView PlaneImg=null;//mentions to AirCraft Image
    
    public F16(Context context)
    {
        this.context = context;
        Image=context.getResources().getIdentifier("F16" , "drawable", context.getPackageName());
    }
    
    public void CreateAirCraft(byte X , byte Y,Context Ctx,LinearLayout Map){
            Name="F-16";    
            PlaneBody=new LinearLayout(Ctx);
            PlaneName=new TextView(Ctx);
            PlaneImg=new ImageView(Ctx);
            PlaneName.setText(Name);
            PlaneImg.setImageResource(Image);
            PlaneBody.addView(PlaneName);
            PlaneBody.addView(PlaneImg);
            Map.addView(PlaneBody);
        }
    }